package network.netty.Package;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import network.netty.message.ChatMessage;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 自定义封包类型
 * </p>
 *
 * @author LovelyBigDuck
 * @date 2021/5/30 8:47
 */
@Getter
@Setter
public class TransferPackageWrap<T> implements LengthTransfer {
    //数据包
    transient public static final byte PACKET_TYPE_DATA = 1;
    //心跳包
    transient public static final byte PACKET_TYPE_MIND = 2;

    transient public static final String PROTOCOL_VERSION = "1.0";

    //数据包类型
    private byte type;

    //时间戳
    private Long timestamp = System.currentTimeMillis() / 1000;

    //协议版本号
    private String version = PROTOCOL_VERSION;

    //数据内容
    private T data;

    public TransferPackageWrap(byte type) {
        this.type = type;
    }

    public TransferPackageWrap(byte type, T data) {
        this.type = type;
        this.data = data;
    }

    /**
     * 隐藏掉要忽略的字段，在序列化的时候不包含该字段
     * @return
     */
    @Override
    @JSONField(serialize = false, deserialize = false)
    public Integer getLength() {
        return this.getBytes().length;
    }

    @Override
    @JSONField(serialize = false, deserialize = false)
    public byte[] getBytes() {
        return this.toJsonString().getBytes(StandardCharsets.UTF_8);
    }


}
